<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">



        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm layui-btn-normal data-add-btn" lay-event="add">同步客服账号</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除客服账号</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

    </div>
</div>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        table.render({
            elem: '#currentTableId',
            url: '/qyapi/kf/account/list',
            method: 'POST',
            contentType: 'application/json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox"},
                {
                    field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                        return d.LAY_INDEX; //返回序号
                    }
                },
                {field: 'openKfid', title: '客服ID', sort: true, align: 'center'},
                {field: 'name', title: '客服名称', sort: true, align: 'center'},
                {field: 'kfUrl', title: '客服链接', sort: true, align: 'center'},
                {field: 'avatar', title: '客服头像', sort: true, align: 'center'},
                {field: 'updateTime', title: '更新时间', sort: true, align: 'center'},
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 50,
            page: true,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            response: { //定义后端 json 格式，详细参见官方文档
                statusName: 'code', //状态字段名称
                statusCode: '200', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total', //总数字段
                dataName: 'rows' //数据字段
            },
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                $.ajax({
                    type:"GET",
                    url:'/qyapi/kf/account/sync',
                    dataType:'json',
                    contentType: 'application/x-www-form-urlencoded',
                    success:function(req){
                        layer.msg(req.msg);
                        reloadTable();
                    },
                    error:function(e){
                        layer.msg("同步失败！");
                    }
                })
            }
            if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗', function (index) {
                        let kfIdList = []
                        for (const i in data) {
                            kfIdList.push(data[i].openKfid);
                        }
                        $.ajax({
                            type: "post",
                            url: '/kf/account/delete',
                            data: JSON.stringify(kfIdList),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code == 200) {
                                    layer.msg("已删除");
                                    reloadTable();
                                } else {
                                    layer.msg("删除失败");
                                }
                            },
                            error: function (e) {
                                layer.msg("删除失败");
                            }
                        })
                        layer.close(index);
                    });
                } else {
                    layer.alert('请至少选中一行数据');
                }
            }
        });

        // 监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });
    });

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/qyapi/kf/account/list'
            });
        })
    }
</script>

</body>
</html>
